package org.groupg.practice.collections;

import org.apache.commons.collections4.CollectionUtils;
import java.util.Arrays;
import java.util.List;

public class CollectionUtilsExample {

    public static void main(String[] args) {
        // 创建两个列表
        List<String> list1 = Arrays.asList("a", "b", "c");
        List<String> list2 = Arrays.asList("b", "c", "d");

        // 求交集
        List<String> intersection = CollectionUtils.intersection(list1, list2).stream().toList();
        System.out.println("Intersection: " + intersection); // 输出: [b, c]

        // 求并集
        List<String> union = CollectionUtils.union(list1, list2).stream().toList();
        System.out.println("Union: " + union); // 输出: [a, b, c, b, c, d] (注意：不会去除重复元素)

        // 求差集
        List<String> subtract = CollectionUtils.subtract(list1, list2).stream().toList();
        System.out.println("Subtract: " + subtract); // 输出: [a]

        // 判断集合是否为空
        boolean isEmpty = CollectionUtils.isEmpty(list1);
        System.out.println("Is list1 empty? " + isEmpty); // 输出: false

        // 判断集合是否相等（不考虑顺序）
        boolean isEqualCollection = CollectionUtils.isEqualCollection(list1, list2);
        System.out.println("Are list1 and list2 equal? " + isEqualCollection); // 输出: false

        // 过滤集合中满足条件的元素
        List<String> filteredList = (List<String>) CollectionUtils.select(list1, item -> item.equals("b"));
        System.out.println("Filtered List: " + filteredList); // 输出: [b]

        // 转换集合中的元素
        List<String> transformedList = (List<String>) CollectionUtils.collect(list1, item -> item.toUpperCase());
        System.out.println("Transformed List: " + transformedList); // 输出: [A, B, C]
    }
}
